package cn.edu.cuit.elena.transaction.system;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import cn.edu.cuit.elena.common.Identifiable;
import cn.edu.cuit.elena.db.DataSource;
import cn.edu.cuit.elena.transaction.DataSourceManager;
import cn.edu.cuit.elena.transaction.query.HiveBaseDao;

public class UserContext
    implements Identifiable
{
    private String userName;
    private String password;

    private Map<String, String> legalModule;
    private Map<String, String[]> modulesTree;
    private DataSourceManager dataSourceManager;
    private HiveBaseDao hive;

    private Cache cache;

    public UserContext(String userName, String password)
    {
        this.userName = userName;
        this.password = password;
        cache = new Cache();
    }

    /**
     * The same as getIdentifier()
     * @see #getIdentifier()
     * */
    public String getUserName()
    {
        return userName;
    }

    public String getPassword()
    {
        return password;
    }

    public Cache getCache()
    {
        return cache;
    }

    /**
     * @return false If there is the specified data source.
     * */
    public boolean caches( DataSource dataSourceToMigrate )
    {
        synchronized( cache.cachesDataSources )
        {
            for( DataSource d : cache.cachesDataSources )
            {
                if( d.toString().equals( dataSourceToMigrate.toString() ) )
                {
                    return false;
                }
            }
            cache.cachesDataSources.add( dataSourceToMigrate );
            return true;
        }
    }

    public void setModules( Map<String, String> legalModule )
    {
        this.legalModule = legalModule;
    }

    public Map<String, String> getModules()
    {
        return legalModule;
    }

    public Map<String, String[]> getModulesTree()
    {
        return modulesTree;
    }

    public void setModulesTree( Map<String, String[]> modulesTree )
    {
        this.modulesTree = modulesTree;
    }

    public DataSourceManager getDataSourceManager()
    {
        return dataSourceManager;
    }

    public void setDataSourceManager( DataSourceManager dataSourceManager )
    {
        this.dataSourceManager = dataSourceManager;
    }

    /** 
     * The same as {@link #getUserName()} 
     * */
    @Override
    public String getIdentifier()
    {
        return userName;
    }

    public void setHiveQuerier( HiveBaseDao hive )
    {
        this.hive = hive;
    }

    public HiveBaseDao getHiveQuerier()
    {
        return hive;
    }

    public final class Cache
    {

        List<DataSource> cachesDataSources;

        Cache()
        {
            cachesDataSources = new LinkedList<DataSource>();
        }

        public List<DataSource> getCachesDataSources()
        {
            return cachesDataSources;
        }

        public void setCachesDataSources( List<DataSource> cachesDataSources )
        {
            this.cachesDataSources = cachesDataSources;
        }

        public DataSource getCacheDataByToString( String toString )
        {
            for( DataSource d : cachesDataSources )
            {
                if( d.toString().equals( toString ) )
                {
                    return d;
                }
            }

            return null;
        }
    }
}
